<?php
/**
 * 用户系统数据连接库
 *
 * @package Appublisher
 * @subpackage Database
 * @author bluer
 * @version 0.3
 */

require_once('db.class.php');
require_once('HyperDB.class.php');
require_once('Builder.class.php');

class UDDB extends HyperDB {
    /**
     * User auth info table
     */
    public $userAuthInfo = 'user_auth_info';

    /**
     * User social info table
     *
     */
    public $userSocialInfo = 'user_social_info';

    /**
     * Dailyplan guest info table
     *
     */
    public $guestInfo = 'dp_guest_info';

    /**
     * User dailyplan data table
     *
     */
    public $userDPData = 'dp_user_dailyplan_data';

    /**
     * DailyPlan conf plan type table
     *
     */
    public $confPlanType = 'dp_conf_plan_type';

    /*
     * DailyPlan recent available mission table
     *
     */
    public $recentAvMission = 'dp_recent_available_mission';

    /**
     * DailyPlan recent mission info table
     *
     */
    public $recentMissionInfo = 'dp_recent_mission_info';

    /**
     * DailyPlan history mission action table
     *
     */
    public $historyMissionAction = 'dp_history_mission_action';

    /**
     * DailyPlan history user tests table
     *
     */
    public $userTests = 'dp_history_user_tests';

    /**
     * 天天模考用户做题记录表
     *
     * @var string
     */
    public $qbUserTests = 'qb_history_user_tests';

    /**
     * Dailyplan collection table
     *
     */
    public $collection = 'dp_dailyplan_collection';

    /**
     * Dailyplan question category table
     *
     */
    public $questionCategory = 'dp_conf_question_category';

    /**
     * 具有分表的表名
     *
     * @var array
     */
    protected $multiTables = array('qb_history_user_tests');


    function __construct() {}

    /**
     * 结构化查询所对应的数据表
     *
     * @param string $name 表名
     * @return Builder
     */
    public function table($name = null) {

        $processor = new Processor();
        $query = new Builder($this, $processor);
        return $query->from($name);
    }


    /**
     * 对qb_history_user_tests根据userId做分表处理
     * @param $userId
     * @return $this|null
     */
    public function usertestsTable($userId) {

        if(empty($userId))
            return null;
        $processor = new Processor();
        $query = new Builder($this,$processor);
        return $query -> from($this -> qbUserTests.'_'.(int)($userId/100000));
    }

    /**
     * 设置使用了分表机制的基本表名
     * @param array|string $tables
     */
    public function setMultiTables($tables) {
        if (is_string($tables)) {
            array_push($this->multiTables, $tables);
        } else if (is_array($tables)) {
            $this->multiTables = array_merge($this->multiTables, $tables);
        }
    }

    /**
     * 获取使用了分表机制的基本表名
     *
     * @return array
     */
    public function getMultiTables() {
        return $this->multiTables;
    }
}

$uddb = new UDDB();

require( CONFIG_DIR . '/hyperdb.config.php' );